#!/bin/sh

option_compile=0
option_output=0
compilation=0
linking=0

if [ -z "$RT_ROOT" ]; then
    RT_ROOT=/usr/local
fi
if [ -z "$RT_TEMPLATE" ]; then
    # default profile
    #RT_TEMPLATE=posix-gcc-mt-file-lint
    RT_TEMPLATE=posix-gcc-mt-netw-lint
fi
TEMPLATE_PATH=$RT_ROOT/lib/restracer/templates/$RT_TEMPLATE

if [ "$RT_DYNAMIC" = "1" ]; then
    LIB_EXT="dynamic/*.so"
else
    LIB_EXT="static/*.o"
fi

for arg in $*; do
   if [ "$arg" = "-c" ]; then option_compile=1;
   elif [ "$arg" = "-o" ]; then option_output=1;
   elif [ "$arg" = "-E" ]; then option_compile=1;
   fi
done

if [ "$option_output" -eq "1" ]; then
    if [ "$option_compile" -eq "1" ]; then
        compilation=1
    else
        linking=1
    fi
elif [ "$option_compile" -eq "1" ]; then
    compilation=1
fi

if [ "$compilation" -eq "1" ]; then
    if [ "$linking" -eq "0" ]; then
        echo "doing -include $TEMPLATE_PATH/art.h"
        c++ -include $TEMPLATE_PATH/art.h $*
    else
        echo "doing -include $TEMPLATE_PATH/art.h $TEMPLATE_PATH/${LIB_EXT}"
        c++ -include $TEMPLATE_PATH/art.h $* $TEMPLATE_PATH/${LIB_EXT}
    fi
elif [ "$linking" -eq "1" ]; then
    echo "doing $TEMPLATE_PATH/${LIB_EXT}"
    c++ $* $TEMPLATE_PATH/${LIB_EXT}
else
    echo "doing -include $TEMPLATE_PATH/art.h"
    c++ -include $TEMPLATE_PATH/art.h $*
fi
